'\" te
.\"  Copyright (c) 2001, Sun Microsystems, Inc.  All Rights Reserved
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH _LWP_COND_WAIT 2 "September 12, 2020"
.SH NAME
_lwp_cond_wait, _lwp_cond_timedwait, _lwp_cond_reltimedwait \- wait on a
condition variable
.SH SYNOPSIS
.nf
#include <sys/lwp.h>

\fBint\fR \fB_lwp_cond_wait\fR(\fBlwp_cond_t *\fR\fIcvp\fR, \fBlwp_mutex_t *\fR\fImp\fR);
.fi

.LP
.nf
\fBint\fR \fB_lwp_cond_timedwait\fR(\fBlwp_cond_t *\fR\fIcvp\fR, \fBlwp_mutex_t *\fR\fImp\fR,
     \fBtimestruc_t *\fR\fIabstime\fR);
.fi

.LP
.nf
\fBint\fR \fB_lwp_cond_reltimedwait\fR(\fBlwp_cond_t *\fR\fIcvp\fR, \fBlwp_mutex_t *\fR\fImp\fR,
     \fBtimestruc_t *\fR\fIreltime\fR);
.fi

.SH DESCRIPTION
These functions are used to wait for the occurrence of a condition represented
by an LWP condition variable. LWP condition variables must be initialized to 0
before use.
.sp
.LP
The \fB_lwp_cond_wait()\fR function atomically releases the LWP mutex pointed
to by \fImp\fR and causes the calling LWP to block on the LWP condition
variable pointed to by \fIcvp\fR. The blocked LWP may be awakened by
\fB_lwp_cond_signal\fR(2), \fB_lwp_cond_broadcast\fR(2), or when interrupted by
delivery of a signal. Any change in value of a condition associated with the
condition variable cannot be inferred by the return of \fB_lwp_cond_wait()\fR
and any such condition must be re-evaluated.
.sp
.LP
The \fB_lwp_cond_timedwait()\fR function is similar to \fB_lwp_cond_wait()\fR,
except that the calling LWP will not block past the time of day specified by
\fIabstime\fR. If the time of day becomes greater than \fIabstime\fR,
\fB_lwp_cond_timedwait()\fR returns with the error code \fBETIME\fR.
.sp
.LP
The \fB_lwp_cond_reltimedwait()\fR function is similar to
\fB_lwp_cond_wait()\fR, except that the calling LWP will not block past the
relative time specified by \fIreltime\fR. If the time of day becomes greater
than the starting time of day plus \fIreltime\fR,
\fB_lwp_cond_reltimedwait()\fR returns with the error code \fBETIME\fR.
.sp
.LP
The \fB_lwp_cond_wait()\fR, \fB_lwp_cond_timedwait()\fR, and
\fB_lwp_cond_reltimedwait()\fR functions always return with the mutex locked
and owned by the calling lightweight process.
.SH RETURN VALUES
Upon successful completion, \fB0\fR is returned. A non-zero value indicates an
error.
.SH ERRORS
If any of the following conditions are detected, \fB_lwp_cond_wait()\fR,
\fB_lwp_cond_timedwait()\fR, and \fB_lwp_cond_reltimedwait()\fR fail and return
the corresponding value:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fIcvp\fR argument points to an invalid LWP condition variable or the
\fImp\fR argument points to an invalid LWP mutex.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 10n
The \fImp\fR, \fIcvp\fR, or \fIabstime\fR argument points to an illegal
address.
.RE

.sp
.LP
If any of the following conditions occur, \fB_lwp_cond_wait()\fR,
\fB_lwp_cond_timedwait()\fR, and \fB_lwp_cond_reltimedwait()\fR fail and return
the corresponding value:
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 9n
The call was interrupted by a signal or \fBfork\fR(2).
.RE

.sp
.LP
If any of the following conditions occur, \fB_lwp_cond_timedwait()\fR and
\fB_lwp_cond_reltimedwait()\fR fail and return the corresponding value:
.sp
.ne 2
.na
\fB\fBETIME\fR\fR
.ad
.RS 9n
The time specified in \fIabstime\fR or \fIreltime\fR has passed.
.RE

.SH EXAMPLES
\fBExample 1 \fRUse the \fB_lwp_cond_wait()\fR function in a loop testing some
condition.
.sp
.LP
The \fB_lwp_cond_wait()\fR function is normally used in a loop testing some
condition, as follows:

.sp
.in +2
.nf
lwp_mutex_t m;
lwp_cond_t cv;
int cond;
(void) _lwp_mutex_lock(&m);
while (cond == FALSE) {
        (void) _lwp_cond_wait(&cv, &m);
}
(void) _lwp_mutex_unlock(&m);
.fi
.in -2

.LP
\fBExample 2 \fRUse the \fB_lwp_cond_timedwait()\fR function in a loop testing
some condition.
.sp
.LP
The \fB_lwp_cond_timedwait()\fR function is also normally used in a loop
testing some condition. It uses an absolute timeout value as follows:

.sp
.in +2
.nf
timestruc_t to;
lwp_mutex_t m;
lwp_cond_t cv;
int cond, err;
(void) _lwp_mutex_lock(&m);
to.tv_sec = time(NULL) + TIMEOUT;
to.tv_nsec = 0;
while (cond == FALSE) {
        err = _lwp_cond_timedwait(&cv, &m, &to);
        if (err == ETIME) {
                /* timeout, do something */
                break;
        }
}
(void) _lwp_mutex_unlock(&m);
.fi
.in -2

.sp
.LP
This example sets a bound on the total wait time even though the
\fB_lwp_cond_timedwait()\fR may return several times due to the condition being
signalled or the wait being interrupted.

.LP
\fBExample 3 \fRUse the \fB_lwp_cond_reltimedwait()\fR function in a loop
testing some condition.
.sp
.LP
The \fB_lwp_cond_reltimedwait()\fR function is also normally used in a loop
testing some condition. It uses a relative timeout value as follows:

.sp
.in +2
.nf
timestruc_t to;
lwp_mutex_t m;
lwp_cond_t cv;
int cond, err;
(void) _lwp_mutex_lock(&m);
while (cond == FALSE) {
        to.tv_sec = TIMEOUT;
        to.tv_nsec = 0;
        err = _lwp_cond_reltimedwait(&cv, &m, &to);
        if (err == ETIME) {
                /* timeout, do something */
                break;
        }
}
(void) _lwp_mutex_unlock(&m);
.fi
.in -2

.SH SEE ALSO
.BR _lwp_cond_broadcast (2),
.BR _lwp_cond_signal (2),
.BR _lwp_kill (2),
.BR _lwp_mutex_lock (2),
.BR fork (2),
.BR kill (2)
